package com.suse.service.view.edu.controller;

import cn.hutool.core.util.ObjectUtil;
import com.suse.common.config.response.ApiException;
import com.suse.common.config.response.R;
import com.suse.common.util.jwt.JwtUtil;
import com.suse.model.dto.UserDto;
import com.suse.service.view.edu.service.CourseCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 课程收藏 前端控制器
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-08-16
 */
@Api(tags = "课程收藏接口")
@RestController
@RequestMapping("/course/collect")
public class CourseCollectController {

    @Resource
    private CourseCollectService courseCollectService;

    @ApiOperation(value = "收藏课程")
    @GetMapping("/auth/save/{courseId}")
    public R saveCollect(@PathVariable("courseId") String courseId, HttpServletRequest request) {
        String token = request.getHeader("token");
        UserDto userDto = JwtUtil.parseToken(token, UserDto.class);
        if (ObjectUtil.isNull(userDto)) {
            throw new ApiException("未登录或登录已过期，请重新登录！");
        }
        String userId = userDto.getId();
        return courseCollectService.saveCollect(courseId,userId);
    }

    @ApiOperation(value = "取消收藏课程")
    @GetMapping("/auth/remove/{courseId}")
    public R removeCollect(@PathVariable("courseId") String courseId, HttpServletRequest request) {
        String token = request.getHeader("token");
        UserDto userDto = JwtUtil.parseToken(token, UserDto.class);
        if (ObjectUtil.isNull(userDto)) {
            throw new ApiException("未登录或登录已过期，请重新登录！");
        }
        String userId = userDto.getId();
        return courseCollectService.removeCollect(courseId,userId);
    }


}

